<template>
  <view class="content">
    <!-- 顶部搜索 begin-->
    <navbar></navbar>
    <!-- 顶部搜索 end-->

    <scroll-view
      class="main"
      scroll-y=""
      @scrolltolower="loadMore"
      refresher-enabled
      :refresher-triggered="refreshing"
      @refresherrefresh="onRefresh"
    >
      <carousel :banners="banners" height="280rpx"></carousel>
      <cate-scroll :category="category"></cate-scroll>
      <!-- 热门推荐 begin-->
      <view class="panel recommend">
        <view class="item" v-for="item in hots" :key="item.id">
          <view class="title">
            {{ item.title }}<text>{{ item.alt }}</text>
          </view>
          <navigator
            hover-class="none"
            :url="`/pages/recommend/index?type=${item.type}`"
            class="cards"
          >
            <image
              mode="aspectFit"
              v-for="img in item.pictures"
              :key="img"
              :src="img"
            ></image>
          </navigator>
        </view>
      </view>
      <!-- 热门推荐 end-->

      <!-- 新鲜好物 begin-->
      <view class="panel fresh">
        <view class="title">
          新鲜好物
          <navigator
            hover-class="none"
            class="more"
            url="/pages/recommend/index?type=5"
            >更多</navigator
          >
        </view>
        <view class="cards">
          <navigator
            hover-class="none"
            :url="`/pages/goods/index?id=${item.id}`"
            v-for="item in newGoods"
            :key="item.id"
          >
            <image mode="aspectFit" :src="item.picture"></image>
            <view class="name">{{ item.name }}</view>
            <view class="price">
              <text class="small">¥</text>{{ item.price }}
            </view>
          </navigator>
        </view>
      </view>
      <!-- 新鲜好物 end-->

      <!-- 热门品牌 begin -->
      <view class="panel brands">
        <view class="title">
          热门品牌
          <navigator hover-class="none" class="more" url="/pages/list/index"
            >更多</navigator
          >
        </view>
        <view class="cards">
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_1.jpg"
            >
            </image>
            <view class="name">小米</view>
            <view class="price">99元起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_2.jpg"
            >
            </image>
            <view class="name">TCL</view>
            <view class="price">199起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_3.jpg"
            >
            </image>
            <view class="name">饭小宝</view>
            <view class="price">9.9起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_4.jpg"
            >
            </image>
            <view class="name">鳄鱼</view>
            <view class="price">299起</view>
          </navigator>
        </view>
      </view>
      <!-- 热门品牌 end -->

      <!-- 猜你喜欢 begin -->
      <guess :likes="likes"></guess>
      <!-- 猜你喜欢 end -->
    </scroll-view>
  </view>
</template>

<script>
import Navbar from "./components/Navbar.vue";
import {
  getBannersAPI,
  getCategoryAPI,
  getHotsAPI,
  getNewGoodsAPI,
  getLikesAPI,
} from "@/api/home";
import Carousel from "@/components/Carousel/Carousel.vue";
import CateScroll from "@/components/CateScroll/CateScroll.vue";
import Guess from "@/components/Guess/Guess.vue";
export default {
  components: { Navbar, Carousel, CateScroll, Guess },
  data() {
    return {
      banners: [],
      category: [],
      hots: [],
      newGoods: [],
      likes: [],
      // 翻页数据准备
      page: 1,
      totalPages: 1,
      isLoading: false,
    };
  },
  onLoad() {
    this.getBannersAPI();
    this.getCategoryAPI();
    this.getHotsAPI();
    this.getNewGoodsAPI();
    this.getLikesAPI();
  },
  methods: {
    // 轮播图
    async getBannersAPI() {
      const { result } = await getBannersAPI();
      this.banners = result;
      // console.log(result);
    },
    // 前台类目
    async getCategoryAPI() {
      const { result } = await getCategoryAPI();
      this.category = result;
      // console.log(result);
    },
    // 人气推荐
    async getHotsAPI() {
      const { result } = await getHotsAPI();
      this.hots = result;
    },
    // 新鲜好物
    async getNewGoodsAPI() {
      const { result } = await getNewGoodsAPI();
      this.newGoods = result;
    },
    // 猜你喜欢
    async getLikesAPI() {
      const {
        result: { items },
        result,
      } = await getLikesAPI(this.page);
      this.likes = items;
      this.totalPages = result.pages;
      // this.likes = [...this.likes, items]
      this.isLoading = false;
    },
    loadMore() {
      if (this.isLoading) return;

      if (this.page >= this.totalPages) return;
      this.page++;
      this.getLikesAPI();
    },
    onRefresh() {
      // 清空旧数据
      this.refreshing = true;
      this.banners = [];
      this.categories = [];
      this.hots = [];
      this.newGoods = [];
      this.likes = [];
      this.loading = false;
      this.page = 1;
      this.pages = 1;

      // 请求新数据
      Promise.all([
        this.getBannersAPI(),
        this.getCategoryAPI(),
        this.getHotsAPI(),
        this.getNewGoodsAPI(),
        this.getLikesAPI(),
      ]).then(() => {
        this.refreshing = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.main {
  flex: 1;
  overflow: hidden;
  background-color: #f7f7f8;
}

/* 公共面板 */
.panel {
  margin: 20rpx 20rpx 0;
  border-radius: 10rpx;
  background-color: #fff;

  .title {
    display: flex;
    align-items: center;
    padding: 24rpx 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    position: relative;

    text {
      font-size: 24rpx;
      color: #7f7f7f;
      margin-left: 18rpx;
    }
  }

  .more {
    position: absolute;
    right: 24rpx;
    font-size: 26rpx;
    color: #7f7f7f;
  }

  .cards {
    display: flex;
  }

  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;

  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254rpx;
    border-right: 1rpx solid #eee;
    border-top: 1rpx solid #eee;

    &:nth-child(2n) {
      border-right: 0 none;
    }

    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }

  .cards {
    flex: 1;
    padding: 15rpx 20rpx;
    justify-content: space-between;

    image {
      width: 48%;
    }
  }
}

/* 新鲜好物 */
.fresh {
  min-height: 320rpx;

  .price {
    line-height: 1;
    text-align: left;
    font-size: 26rpx;
    color: #cf4444;
  }

  .small {
    font-size: 80%;
  }
}

.fresh .cards navigator,
.brands .cards navigator {
  width: 25%;
  padding: 15rpx 20rpx 20rpx;
  line-height: 1;
  text-align: center;
}

.fresh image,
.brands image {
  width: 126rpx;
  height: 126rpx;
}

.fresh .name,
.brands .name {
  margin: 20rpx 0 16rpx;
  font-size: 24rpx;
  color: #262626;
}

.brands {
  .price {
    font-size: 24rpx;
    color: #999;
  }
}
</style>
